const app = getApp();
import {
    postIndexData,
    postuserintegraltaskData,
    receivetaskintegral,
    integral_rank
} from '../../api/store.js';
import {
    setFormId,
    getCoupons,
    exchange
} from '../../api/api.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '0',
            'title': '壹米兔',
            'color': false
        },
        bg: true,
        isGoIndex: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            navH: app.globalData.navHeight
        });
        if (app.globalData.token) that.setData({
            iShidden: true
        });
    },
    onLoadFun: function() {
        this.getIndexConfig()
        this.postuserintegral()
        this.integral_rank()
        this.getCoupons()
    },
    /**
     * 关闭授权
     * 
     */
    onCloseAuto: function() {
        this.setData({
            iShidden: true
        });
    },

    one: function() {
        this.setData({
            bg: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (app.globalData.isLog == true) {
            this.getIndexConfig()
            this.postuserintegral()
            this.integral_rank()
            this.setData({
                iShidden: true
            });
        }
    },

    // 积分兑换
    getIndexConfig: function() {
        // var that = this;
        // postIndexData(1, 4).then(res => {
        //     that.setData({
        //         list: res.data
        //     })

        // })
    },
    operation: function(e) {
        var id = e.currentTarget.dataset.id;
        if (id == 1) {
            wx.navigateTo({
                url: '/pages/user_sgin/index',
            })
        }else if(id == 4 || id == 6){
            wx.switchTab({
                url: '/pages/index/index',
            })
        }

    },
    // 积分任务
    postuserintegral: function() {
        var that = this;
        postuserintegraltaskData().then(res => {
            var task = res.data
            for (var index in task) {
                task[index]['bindtap'] = "";
                //定位class
                if (index == 0) {
                    task[index]['classname'] = "jf one";
                } else if (index == 1) {
                    task[index]['classname'] = "jf two";
                } else if (index == 2) {
                    task[index]['classname'] = "jf three";
                } else if (index == 3) {
                    task[index]['classname'] = "jf four";
                } else if (index == 4) {
                    task[index]['classname'] = "jf five";
                }

                //状态class
                if (task[index]['status'] == 0) { //已完成
                    task[index]['classname'] += " Wcomplete";
                    task[index]['bindtap'] = "Receive";
                } else if (task[index]['status'] == null || task[index]['status'] == '') { //未完成
                    task[index]['classname'] += " Nbgurl ishidden";
                } else if (task[index]['status']) { //已领取
                    task[index]['classname'] += " Ycomplete";
                }

            }
            that.setData({
                task: task
            })
        })
    },

    Receive: function(option) {
        var id = option.currentTarget.dataset.id
        var that = this;
        receivetaskintegral(id).then(res => {
            if (res.status == '200') {
                app.Tips({
                    title: '领取成功！',
                    icon: 'success'
                });
                that.postuserintegral();
            } else {
                app.Tips({
                    title: '领取失败！'
                });
            }
        })
    },

    // 排行
    integral_rank: function(page = 1, limit = 4) {
        var that = this;
        integral_rank(page, limit).then(res => {
            var rank = res.data.rank
            that.setData({
                rank: rank
            })
        })
    },
    // 积分列表
    getCoupons: function() {
        var that = this
        getCoupons({
            page: 1,
            limit: 3,
            type: 2,
        }).then(res => {
            that.setData({
                couponList: res.data
            })
        });
    },
    // 兑换积分
    exchange: function(e) {
        wx.showModal({
            title: '提示',
            content: '确认兑换优惠券',
            success: function(res) {
                if (res.confirm) {
                    exchange({
                        couponId: e.currentTarget.dataset.id
                    }).then(res => {
                        return app.Tips({
                            title: res.msg
                        })
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    // console.log('用户点击取消')
                }
            }
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})